package com.cslg.ac.demo.service.file;
import com.alibaba.fastjson.JSON;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.host.HostDAO;
import com.cslg.ac.demo.dao.host.po.HostPO;
import com.cslg.ac.demo.dao.pet.PetAdoptDAO;
import com.cslg.ac.demo.dao.pet.PetCommonDAO;
import com.cslg.ac.demo.dao.pet.PetFosterDAO;
import com.cslg.ac.demo.dao.pet.po.PetAdoptPO;
import com.cslg.ac.demo.dao.pet.po.PetCommonPO;
import com.cslg.ac.demo.dao.pet.po.PetFosterPO;
import com.cslg.ac.demo.dao.post.PostDAO;
import com.cslg.ac.demo.dao.post.po.PostPO;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.po.team.TeamHeadPO;
import com.cslg.ac.demo.dao.user.po.team.TeamProfilePO;
import com.cslg.ac.demo.dao.user.team.TeamHeadDAO;
import com.cslg.ac.demo.dao.user.team.TeamInfoDAO;
import com.cslg.ac.demo.dao.user.po.person.PersonInfoPO;
import com.cslg.ac.demo.dao.user.po.team.TeamInfoPO;
import com.cslg.ac.demo.dao.user.team.TeamProfileDAO;
import com.cslg.ac.demo.util.communicate.CosClientUtil;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import com.cslg.ac.demo.util.string.StringUtil;
import com.qcloud.cos.COS;
import com.qcloud.cos.COSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
@PropertySource(value = {"classpath:file.properties"},encoding="utf-8")
public class FileService {
   @Value("${imageBucket}")
    String imageBucket;

    @Value("${teamType}")
    int teamType;
    @Value("${personType}")
    int personType;
    @Value("${petType}")
    int petType;
    @Value("${hostType}")
    int hostType;
    @Value("${albumBucket}")
    String albumBucket;
    @Value("${textBucket}")
    String textBucket;
    @Value("${photoBucket}")
    String photoBucket;
    @Value("${videoBucket}")
    String videoBucket;


    @Autowired
    TeamInfoDAO teamInfoDAO;
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Autowired
    PetFosterDAO petFosterDAO;
    @Autowired
    PetCommonDAO petCommonDAO;
    @Autowired
    TeamProfileDAO teamProfileDAO;
    @Autowired
    TeamHeadDAO teamHeadDAO;
    @Autowired
    PetAdoptDAO petAdoptDAO;
    @Autowired
    PostDAO postDAO;
    @Autowired
    HostDAO hostDAO;


    public String getName(int userType, int id ){
        if(userType == teamType){
            return "/team_" + id;
        }else if(userType == personType){
            return "/person_" + id;
        }else if(userType == petType){
            return "/pet_" + id;
        }else if(userType == hostType){
            return "/host_" + id;
        }else {
            return null;
        }
    }

    public String downloadVideo(int id,int userType,int videoType){
        String name = getName(userType,id);
        if(name == null){
            return null;
        }
        name += "_video_" + videoType;
        if(userType == personType){
            //暂不支持
            return null;
        }else if(userType == teamType){
            return null;
        }else if(userType == petType){
            PetAdoptPO petAdoptPO = petAdoptDAO.selectById(id);
            if(petAdoptPO == null){
                return null;
            }
            return CosClientUtil.GenerateSimplePresignedDownloadUrl(name,videoBucket);
        }
        return null;
    }

    /**
     *
     * @param id
     * @param userType
     * @param videoType 0:待领养宠物的视频
     * @return
     */
    public int uploadVideo(int id, int userType, int videoType, MultipartFile video){
        String name = getName(userType,id);
        if(name == null){
            return ResponseCode.PARAM_ILLEGAL;
        }
        name += "_video_" + videoType;
        if(userType == personType){
            //暂时不支持
            return ResponseCode.PARAM_ILLEGAL;
        }else if(userType == teamType){
            //暂时不支持
            return ResponseCode.PARAM_ILLEGAL;
        }else if(userType == petType){
            PetAdoptPO petAdoptPO = petAdoptDAO.selectById(id);
            if(petAdoptPO == null){
                return ResponseCode.USER_NOT_EXISTS;
            }
            petAdoptDAO.updateVideo(id,name.substring(1));
        }else {
            return ResponseCode.PARAM_ILLEGAL;
        }
        CosClientUtil.uploadFile(name,1,video);
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public String downloadPhoto(int id,int userType,int photoType){
        String name = getName(userType,id);
        if(name == null){
            return null;
        }
        name += "_photo_" + photoType;
        if(userType == personType){
            //暂时不支持
            return null;
        }else if(userType == petType){
            //暂时不支持
            return null;
        }else if(userType == hostType && photoType == 3){
            HostPO hostPO = hostDAO.selectById(id);
            if(hostPO == null || StringUtil.haveNullOrEmpty(hostPO.getPetImage())){
                System.out.println("testew");
                return null;
            }
        }else if(userType == teamType){
            if(photoType == 0 || photoType == 1){
                TeamHeadPO teamHeadPO = teamHeadDAO.selectById(id);
                boolean flag = (teamHeadPO == null ||photoType == 0 && StringUtil.haveNullOrEmpty(teamHeadPO.getIdCard0()) || photoType == 1
                && StringUtil.haveNullOrEmpty(teamHeadPO.getIdCard1()));
                if(flag){
                    return null;
                }
            }else if(photoType == 2){
                TeamInfoPO teamInfoPO = teamInfoDAO.selectById(id);
                if(teamInfoPO == null || StringUtil.haveNullOrEmpty(teamInfoPO.getProof())){
                    return null;
                }
            }else {
                return null;
            }
        }else {
            return null;
        }
        return CosClientUtil.GenerateSimplePresignedDownloadUrl(name,photoBucket);
    }


    /**
     *
     * @param id
     * @param userType
     * @param photoType 0：身份证正面 1：身份证反面 2：组织证明 3：宠物头像（寄养家庭中的）
     * @return
     */
    public int uploadPhoto(MultipartFile photo,int id,int userType,int photoType){
        String name = getName(userType,id);
        if(name == null){
            return ResponseCode.PARAM_ILLEGAL;
        }
        name += "_photo_" + photoType;
        if(userType == personType){
            //暂时不支持
            return ResponseCode.PARAM_ILLEGAL;
        }else if(userType == hostType){
            HostPO hostPO = hostDAO.selectById(id);
            if(hostPO == null || photoType != 3){
                return ResponseCode.USER_NOT_EXISTS;
            }
            hostDAO.updatePetImage(name.substring(1),id);
        }else if(userType == teamType){
            if(photoType == 0 || photoType == 1){
                TeamHeadPO teamHeadPO = teamHeadDAO.selectById(id);
                if(teamHeadPO == null){
                    return ResponseCode.USER_NOT_EXISTS;
                }
                if(photoType == 0){
                    teamHeadDAO.updateIdCard0(id,name.substring(1));
                }else{
                    teamHeadDAO.updateIdCard1(id,name.substring(1));
                }
            }else if(photoType == 2){
                TeamInfoPO teamInfoPO = teamInfoDAO.selectById(id);
                if(teamInfoPO == null){
                    return ResponseCode.USER_NOT_EXISTS;
                }
                teamInfoDAO.updateProof(id,name.substring(1));
            }else {
                return ResponseCode.PARAM_ILLEGAL;
            }
        } else {
            return ResponseCode.PARAM_ILLEGAL;
        }
        CosClientUtil.uploadFile(name,4,photo);
        return ResponseCode.REQUEST_SUCCESSED;

    }

    public String downloadText(int id,int userType,int textType){
        String name = getName(userType,id);
        if(name == null){
            return null;
        }
        name += "_text_" + textType;
        boolean flag;
        if(userType == personType){
        //暂时不可用
            return null;
        }else if(userType == petType){
            //暂时不可用
            return null;
        }else if(userType == hostType){
            //暂时不可用
            return null;
        }else if(userType == teamType){
            if(textType == 0 || textType == 1){
                TeamProfilePO teamProfilePO = teamProfileDAO.selectById(id);
                flag = (teamProfilePO == null ) || (textType == 0 && StringUtil.haveNullOrEmpty(teamProfilePO.getJobRules())) ||
                        (textType == 1 && StringUtil.haveNullOrEmpty(teamProfilePO.getAdoptRules()));
                if(flag){
                    return null;
                }
            }else if(textType == 2){
                TeamHeadPO teamHeadPO = teamHeadDAO.selectById(id);
                flag = (teamHeadPO == null || StringUtil.haveNullOrEmpty(teamHeadPO.gethIntroduction()));
                if(flag){
                    return null;
                }
            }else{
                return null;
            }

        }else {
            return null;
        }
        return CosClientUtil.GenerateSimplePresignedDownloadUrl(name,textBucket);

    }

    /**
     *
     * @param id
     * @param userType
     * @param textType  0 是 jobRules，1 是 adoptRules, 2是hIntroduction
     * @return
     */
    public int uploadText(MultipartFile text,int id,int userType,int textType){
        String name = getName(userType,id) + "_text_" + textType;
        if(userType == personType){
            //暂时还不支持此功能
            return ResponseCode.PARAM_ILLEGAL;
        }else if(userType == petType){
            //暂时不支持
            return ResponseCode.PARAM_ILLEGAL;
        }else if(userType == hostType){
            //暂时不支持
            return ResponseCode.PARAM_ILLEGAL;
        }else if(userType == teamType){
            if(textType == 0 || textType == 1){
                TeamProfilePO teamProfilePO = teamProfileDAO.selectById(id);
                if(teamProfilePO == null){
                    return ResponseCode.USER_NOT_EXISTS;
                }
                if(textType == 0){
                    teamProfileDAO.updateJobRule(id,name.substring(1));
                }else {
                    teamProfileDAO.updateAdoptRule(id,name.substring(1));
                }
            }else if(textType == 2){
                TeamHeadPO teamHeadPO = teamHeadDAO.selectById(id);
                if(teamHeadPO == null){
                    return ResponseCode.USER_NOT_EXISTS;
                }
                teamHeadDAO.updateIntroduction(name.substring(1));
            }else {
                return ResponseCode.PARAM_ILLEGAL;
            }
        }else {
            return ResponseCode.PARAM_ILLEGAL;
        }
        CosClientUtil.uploadFile(name,3,text);
        return ResponseCode.REQUEST_SUCCESSED;
    }



    public String downloadImage(int id,int type){
        String name = getName(type, id);
        if(name == null){
            return null;
        }
        if(type == personType) {
            PersonInfoPO personInfoPO = personInfoDAO.selectById(id);
            if (personInfoPO == null || StringUtil.haveNullOrEmpty(personInfoPO.getImage())) {
                return null;
            }
        }else if(type == teamType) {
            TeamInfoPO teamInfoPO = teamInfoDAO.selectById(id);
            if (teamInfoPO == null || StringUtil.haveNullOrEmpty(teamInfoPO.getImage())) {
                return null;
            }
        }else if(type == petType){
            PetCommonPO petCommonPO = petCommonDAO.selectById(id);
               if(petCommonPO == null || StringUtil.haveNullOrEmpty(petCommonPO.getImage())){
                   return null;
               }
            }else if(type == hostType){
            HostPO hostPO = hostDAO.selectById(id);
            if(hostPO == null || StringUtil.haveNullOrEmpty(hostPO.getImage())){
                return null;
            }
        }
        return CosClientUtil.GenerateSimplePresignedDownloadUrl(name + "_image",imageBucket);
    }

    public int uploadImage(MultipartFile image,int id,int type){
        String  name = getName(type, id);
        if(name == null){
            return ResponseCode.PARAM_ILLEGAL;
        }
        name +=  "_image";
        if(type == teamType){
            TeamInfoPO teamInfoPO = teamInfoDAO.selectById(id);
            if(teamInfoPO == null){
                return ResponseCode.USER_NOT_EXISTS;
            }
            teamInfoDAO.updateImage(id,name.substring(1));
        }else if(type == personType){
            PersonInfoPO personInfoPO = personInfoDAO.selectById(id);
            if(personInfoPO == null){
                return ResponseCode.USER_NOT_EXISTS;
            }
            personInfoPO.setImage(name.substring(1));
            personInfoDAO.updateAllById(personInfoPO);
        }else if(type == petType){
            PetCommonPO petCommonPO = petCommonDAO.selectById(id);
            if(petCommonPO == null){
                return ResponseCode.USER_NOT_EXISTS;
            }
            petCommonPO.setImage(name.substring(1));
            petCommonDAO.updateById(petCommonPO);
        }else if(type == hostType){
            HostPO hostPO = hostDAO.selectById(id);
            if(hostPO == null){
                return ResponseCode.USER_NOT_EXISTS;
            }
            hostDAO.updateImage(name.substring(1),id);

        } else {
            return ResponseCode.PARAM_ILLEGAL;
        }
        CosClientUtil.uploadFile(name,0,image);
        return ResponseCode.REQUEST_SUCCESSED;
    }

    /**
     *
     * @param id
     * @param userType
     * @param albumType
     * @return
     */
    public List<String> downloadAlbum(int id,int userType,int albumType) {
        String name = getName(userType,id);
        if (name == null) {
            return null;
        }
        name += "_album_" + albumType;
        name = name.substring(1);
        List<String> url = new ArrayList<>();
        boolean flag = (userType == personType || userType == teamType) && albumType == 0;
        if(flag){
            PostPO postPO = postDAO.selectById(id);
            if(postPO == null || postPO.getAlbumCount() == 0){
                return null;
            }
            for(int i = 0;i < postPO.getAlbumCount();i++){
                url.add(CosClientUtil.GenerateSimplePresignedDownloadUrl(name + "_" + i,albumBucket));
            }
            return url;
        }else if(userType == petType && albumType == 1){
            PetFosterPO petFosterPO = petFosterDAO.selectById(id);
            if(petFosterPO == null || petFosterPO.getAlbumCount() == 0){
                return null;
            }
            for(int i = 0;i < petFosterPO.getAlbumCount();i++){
                url.add(CosClientUtil.GenerateSimplePresignedDownloadUrl(name + "_" + i,albumBucket));
            }
            return url;
        }
        return null;
    }


    /**
     *
     * @param album
     * @param id
     * @param userType
     * @param albumType 0为社区动态，1为相册
     * @return
     */
    public int uploadAlbum(MultipartFile[] album,int id,int userType,int albumType){
        String name = getName(userType, id);
        PostPO postPO;
        PetFosterPO petFosterPO;
        if(name == null){
            return ResponseCode.PARAM_ILLEGAL;
        }
        name += "_album_" + albumType;
        boolean flag = (userType == personType || userType == teamType) && (albumType == 0);
        if(flag){
                postPO = postDAO.selectById(id);
                if(postPO == null){
                    return ResponseCode.USER_NOT_EXISTS;
                }
                if(postPO.getAlbumCount() != 0){
                    return ResponseCode.FILE_HAVE_UPLOAD;
                }
            for(int i = 0;i < album.length;i++){
                CosClientUtil.uploadFile(name + "_" + i,2,album[i]);
            }
            postPO.setAlbumCount(album.length);
            postDAO.updateById(postPO);
        }else if(userType == petType && albumType == 1){
            petFosterPO = petFosterDAO.selectById(id);
            if(petFosterPO == null){
                return ResponseCode.USER_NOT_EXISTS;
            }
            if(petFosterPO.getAlbumCount() != 0){
                return ResponseCode.FILE_HAVE_UPLOAD;
            }
            for(int i = 0;i < album.length;i++){
                CosClientUtil.uploadFile(name + "_" + i,2,album[i]);
            }
            petFosterPO.setAlbumCount(album.length);
            petFosterDAO.updateAlbum(petFosterPO);
        }else {
            return ResponseCode.PARAM_ILLEGAL;
        }

        return ResponseCode.REQUEST_SUCCESSED;

    }



}
